package com.lark.utils;

import javafx.scene.Node;
import javafx.stage.Stage;

import java.util.concurrent.atomic.AtomicReference;

public final class NodeUtil {

    public static void drag(Node node, Stage stage) {
        AtomicReference<Double> offsetX = new AtomicReference<>((double) 0);
        AtomicReference<Double> offsetY = new AtomicReference<>((double) 0);
        node.setOnMousePressed(mouseEvent -> {
            // 记录鼠标按下时在scene中的偏移量
            offsetX.set(mouseEvent.getSceneX());
            offsetY.set(mouseEvent.getSceneY());
        });
        node.setOnMouseDragged(mouseEvent -> {
            // 鼠标在屏幕中的当前位置减去偏移量即要移动的位置
            stage.setX(mouseEvent.getScreenX() - offsetX.get());
            stage.setY(mouseEvent.getScreenY() - offsetY.get());
        });
    }
}
